﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace linkouqs.Gamekit
{
    public class SelectCharacterItem : MonoBehaviour
    {
        public int ID;
        private CharacterController2D c2d;

        private Button characterItemBtn;

        public int GetCharacterID()
        {
            return c2d.index+1;
        }

        public void UseItemClcik()
        {
            if (InventoryManager.Instance.PickedItem == null) return;

            Item item = InventoryManager.Instance.PickedItem.Item;
            GameObject tempGo = Resources.Load<GameObject>(item.GoPath);
            

            switch(InventoryManager.Instance.PickedItem.Item.ItemType)
            {
                case ItemType.NullItemType:
                    Debug.LogWarning("你使用的物品没有类型");
                    break;
                case ItemType.Drug:
                    Drug drug = InventoryManager.Instance.PickedItem.Item as Drug;
                    c2d.gameObject.GetComponent<CharacterHealth>().CurrentHealth += drug.HP;
                   
                    break;
                case ItemType.UnConsumableProp:
                    EquipmentSlot slot=   Knaspack.Instance.GetEquipmentSlotByID(GetCharacterID());
                    Knaspack.Instance.PutOn(item,slot);
                   //TODO 使用物品
                    break;
                case ItemType.Weapon:
                    slot = Knaspack.Instance.GetEquipmentSlotByID(GetCharacterID());
                    Knaspack.Instance.PutOn(item, slot);
                    //TODO 使用武器
                    break;
                case ItemType.ConsumableProp:
                   
                    GameObject prop=GameObject.Instantiate(tempGo,GameObject.Find("Environments").transform.Find("Ground"));

                    if(prop.activeInHierarchy==false)
                    {
                        prop.SetActive(true);
                    }

                    TakeableObject tempTake = prop.GetComponent<TakeableObject>();
                    c2d.gameObject.GetComponent<CharacterAbilityTake>().OnSelect(tempTake);
                    c2d.gameObject.GetComponent<CharacterAbilityTake>().StartAction();

                    break;
                default:
                    Debug.LogWarning("使用物品出错");
                    break;

            }
            InventoryManager.Instance.RemoveItem();
            UIManager.Instance.PopPanel();
        }

        private void Start()
        {
            CharacterController2D[] c2ds= GameObject.FindObjectsOfType<CharacterController2D>();
            foreach(CharacterController2D temp in c2ds)
            {
                if(ID==temp.index+1)
                {
                    c2d = temp;
                    break;
                }
            }

            characterItemBtn = this.GetComponent<Button>();
            characterItemBtn.onClick.AddListener(UseItemClcik);
        }
    }
}
